package com.wpzf.ndhc.controller;

import com.wpzf.common.core.utils.ServletUtils;
import com.wpzf.common.core.web.domain.AjaxResult;
import com.wpzf.common.log.annotation.Log;
import com.wpzf.common.log.enums.BusinessType;
import com.wpzf.common.security.service.TokenService;
import com.wpzf.ndhc.service.WpzfNdhcAPPWyjzService;
import com.wpzf.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wpzfndhcwyrw")
public class WpzfNdhcAPPWyjzController {

    @Autowired
    private WpzfNdhcAPPWyjzService wpzfNdhcAPPWyjzService;

    @Autowired
    TokenService tokenService;

    @Log(title = "获取字典值", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/getVueDictionary", method = RequestMethod.POST)
    public List<Map<String, Object>> getVueDictionary(@RequestBody String[] arrayParam) {
        List<Map<String, Object>> rest = new ArrayList<>();
        if (arrayParam.length > 0) {
            for (int i = 0; i < arrayParam.length; i++) {
                Map<String, Object> map = new HashMap<>();
                String key = arrayParam[i].toString();
                Map<String, Object> m = wpzfNdhcAPPWyjzService.getsql(key);
                map.put("Config", m.get("Config"));
                map.put("DicNo", m.get("DicNo"));
                List<Map<String, Object>> data = wpzfNdhcAPPWyjzService.executeQuery(m);
                if (data != null && data.size() > 0) {
                    map.put("data", data);
                } else {
                    List<Map<String, Object>> list = wpzfNdhcAPPWyjzService.queryDictionarylist(key);
                    map.put("data", list);
                }
                rest.add(map);
            }
        }
        return rest;
    }

    //1.获取任务列表  http://localhost:9991/api/re_task/getPageData
    @RequestMapping(value = "/getPageData", method = RequestMethod.POST)
    public Map<String, Object> getPageData(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("user_id", loginUser.getSysUser().getUserId());
        param.put("xzqdm", loginUser.getSysUser().getXzqdm());
        String xzqdmjq = loginUser.getSysUser().getXzqdmjq();
        String[] xzqdmjqArray = null;
            if (xzqdmjq != null && !"".equals(xzqdmjq)) {
                xzqdmjqArray = xzqdmjq.split(",");
            }
        param.put("xzqdmjqArray", xzqdmjqArray);
        Map<String, Object> rest = new HashMap<>();
        rest.put("status", "0");
        rest.put("msg", null);
        List<Map<String, Object>> rowsList = wpzfNdhcAPPWyjzService.getPageData(param);
        rest.put("rows", rowsList);
        Integer cnt = wpzfNdhcAPPWyjzService.getPageDataCount(param);
        rest.put("total", cnt);
        rest.put("summary", null);
        rest.put("extra", null);
        return rest;
    }

    //任务列表详情
    @RequestMapping(value = "/getTaskDetails", method = RequestMethod.POST)
    public AjaxResult getTaskDetails(@RequestBody Map<String, Object> param) {
        List<Map<String, Object>> rest = wpzfNdhcAPPWyjzService.getTaskDetails(param);
        Integer cnt = wpzfNdhcAPPWyjzService.getTaskDetailsCount(param);
        if (rest != null) {
            return AjaxResult.success("任务列表详情 成功", rest, cnt);
        }
        return AjaxResult.error("任务列表详情 失败", null);
    }

    //查询任务添加地块列表
    @RequestMapping(value = "/queryAddDkList", method = RequestMethod.POST)
    public AjaxResult queryAddDkList(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("xzqdm", loginUser.getSysUser().getXzqdm());
        List<Map<String, Object>> rest = wpzfNdhcAPPWyjzService.queryAddDkList(param);
        Integer cnt = wpzfNdhcAPPWyjzService.queryAddDkListCount(param);
        if (rest != null) {
            return AjaxResult.success("查询任务添加地块列表 成功", rest, cnt);
        }
        return AjaxResult.error("查询任务添加地块列表 失败", null);
    }

    //任务添加图斑
    @RequestMapping(value = "/addTaskDetails", method = RequestMethod.POST)
    public AjaxResult addTaskDetails(@RequestBody Map<String, Object> param) {
        Integer rest = wpzfNdhcAPPWyjzService.addTaskDetails(param);
        if (rest != null) {
            return AjaxResult.success("任务添加图斑 成功", rest);
        }
        return AjaxResult.error("任务添加图斑 失败", null);
    }

    //2.编辑任务  http://localhost:9991/api/re_task/update
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Map<String, Object> update(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("modifyid", loginUser.getSysUser().getUserId());
        param.put("modifier", loginUser.getSysUser().getUserName());
        param.put("modifydate", null);

        Map<String, Object> rest = new HashMap<>();
        Integer i = wpzfNdhcAPPWyjzService.update(param);
        if (i == -1) {
            rest.put("code", -1);
            rest.put("message", "任务名称已存在");
        } else if (i > 0) {
            rest.put("code", 200);
            rest.put("message", "更新成功");
        } else {
            rest.put("code", -1);
            rest.put("message", "更新失败");
        }
        return rest;
    }

    // 查询批次号
    @RequestMapping(value = "/getPch", method = RequestMethod.POST)
    public AjaxResult getPch(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("xzqdm", loginUser.getSysUser().getXzqdm());
        List<Map<String, Object>> rest = wpzfNdhcAPPWyjzService.getPch(param);
        return AjaxResult.success("查询批次号", rest);
    }

    // 省
    @RequestMapping(value = "/getSheng", method = RequestMethod.POST)
    public Map<String, Object> getSheng(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("xzqdm", loginUser.getSysUser().getXzqdm());
        Map<String, Object> rest = new HashMap<>();
        List<Map<String, Object>> list = wpzfNdhcAPPWyjzService.getSheng(param);
        rest.put("code", 200);
        rest.put("data", list);
        rest.put("message", "查询成功");
        return rest;
    }

    // 市
    @RequestMapping(value = "/getShi", method = RequestMethod.POST)
    public Map<String, Object> getShi(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("xzqdm", loginUser.getSysUser().getXzqdm());
        String xzqdmjq = loginUser.getSysUser().getXzqdmjq();
        String[] xzqdmjqArray = null;
            if (xzqdmjq != null && !"".equals(xzqdmjq)) {
                xzqdmjqArray = xzqdmjq.split(",");
            }
        param.put("xzqdmjqArray", xzqdmjqArray);
        Map<String, Object> rest = new HashMap<>();
        List<Map<String, Object>> list = wpzfNdhcAPPWyjzService.getShi(param);
        rest.put("code", 200);
        rest.put("data", list);
        rest.put("message", "查询成功");
        return rest;
    }

    // 县
    @RequestMapping(value = "/getXian", method = RequestMethod.POST)
    public Map<String, Object> getXian(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("xzqdm", loginUser.getSysUser().getXzqdm());
        Map<String, Object> rest = new HashMap<>();
        List<Map<String, Object>> list = wpzfNdhcAPPWyjzService.getXian(param);
        rest.put("code", 200);
        rest.put("data", list);
        rest.put("message", "查询成功");
        return rest;
    }

    // 乡镇
    @RequestMapping(value = "/getXz", method = RequestMethod.POST)
    public Map<String, Object> getXz(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("xzqdm", loginUser.getSysUser().getXzqdm());
        Map<String, Object> rest = new HashMap<>();
        List<Map<String, Object>> list = wpzfNdhcAPPWyjzService.getXz(param);
        rest.put("code", 200);
        rest.put("data", list);
        rest.put("message", "查询成功");
        return rest;
    }

    // 村
    @RequestMapping(value = "/getCun", method = RequestMethod.POST)
    public Map<String, Object> getCun(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("xzqdm", loginUser.getSysUser().getXzqdm());
        Map<String, Object> rest = new HashMap<>();
        List<Map<String, Object>> list = wpzfNdhcAPPWyjzService.getCun(param);
        rest.put("code", 200);
        rest.put("data", list);
        rest.put("message", "查询成功");
        return rest;
    }

    //3.删除任务  http://localhost:9991/api/re_task/del
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Map<String, Object> del(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("modifyid", loginUser.getSysUser().getUserId());
        param.put("modifier", loginUser.getSysUser().getUserName());
        param.put("modifydate", null);

        Map<String, Object> rest = new HashMap<>();
        Integer i = wpzfNdhcAPPWyjzService.del(param);
        if (i > 0) {
            rest.put("code", 200);
            rest.put("message", "删除成功");
        } else {
            rest.put("code", -1);
            rest.put("message", "删除失败");
        }
        return rest;
    }

    //4.图斑列表  http://localhost:9991/api/re_taskdetail_bgdc2022/GetDataNoAssigning
    @RequestMapping(value = "/getDataNoAssigning", method = RequestMethod.POST)
    public Map<String, Object> getDataNoAssigning(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("xzqdm", loginUser.getSysUser().getXzqdm());
        Map<String, Object> rest = new HashMap<>();
        List<Map<String, Object>> rows = wpzfNdhcAPPWyjzService.getDataNoAssigning(param);
        rest.put("rows", rows);
        Integer cnt = wpzfNdhcAPPWyjzService.getDataNoAssigningCount(param);
        rest.put("total", cnt);
        rest.put("code", 200);
        rest.put("message", "查询成功");
        return rest;
    }

    //5.上传文件  http://localhost:9991/api/re_taskdetail_bgdc2022/Upload
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Map<String, Object> upload(@RequestBody Map<String, Object> param) {
        Map<String, Object> rest = new HashMap<>();
        rest.put("msg", "");
        return rest;
    }

    //6.导入图斑  http://localhost:9991/api/User/importshp
    @RequestMapping(value = "/importshp", method = RequestMethod.POST)
    public Map<String, Object> importshp(@RequestBody Map<String, Object> param) {
        Map<String, Object> rest = new HashMap<>();
        rest.put("msg", "");
        return rest;
    }

    //指派人员
    @RequestMapping(value = "/getAssignPersonnel", method = RequestMethod.POST)
    public Map<String, Object> getAssignPersonnel(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("xzqdm", loginUser.getSysUser().getXzqdm());
        Map<String, Object> rest = new HashMap<>();
        List<Map<String, Object>> data = wpzfNdhcAPPWyjzService.getAssignPersonnel(param);
        rest.put("code", 200);
        rest.put("data", data);
        rest.put("message", "查询成功");
        return rest;
    }

    //7.任务分配
    @RequestMapping(value = "/addTask", method = RequestMethod.POST)
    public Map<String, Object> addTask(@RequestBody Map<String, Object> param) {
        Map<String, Object> rest = new HashMap<>();
        Integer i = wpzfNdhcAPPWyjzService.addTask(param);
        if (i > 0) {
            rest.put("code", 200);
            rest.put("msg", "任务分配成功");
        } else {
            rest.put("code", -1);
            rest.put("msg", "任务分配失败");
        }
        return rest;
    }

    //8.移除
    @RequestMapping(value = "/removeTask", method = RequestMethod.POST)
    public Map<String, Object> removeTask(@RequestBody Map<String, Object> param) {
        Map<String, Object> rest = new HashMap<>();
        Integer i = wpzfNdhcAPPWyjzService.removeTask(param);
        if (i > 0) {
            rest.put("code", 200);
            rest.put("msg", "移除成功");
        } else {
            rest.put("code", -1);
            rest.put("msg", "移除失败");
        }
        return rest;
    }

    //查询省
    @RequestMapping(value = "/querySheng", method = RequestMethod.POST)
    public AjaxResult querySheng(@RequestBody Map<String, Object> param) {
        List<Map<String, Object>> rest = wpzfNdhcAPPWyjzService.querySheng(param);
        if (rest != null) {
            return AjaxResult.success("查询省 成功", rest);
        }
        return AjaxResult.error("查询省 失败", -1);
    }

    //查询市
    @RequestMapping(value = "/queryShi", method = RequestMethod.POST)
    public AjaxResult queryShi(@RequestBody Map<String, Object> param) {
        List<Map<String, Object>> rest = wpzfNdhcAPPWyjzService.queryShi(param);
        if (rest != null) {
            return AjaxResult.success("查询市 成功", rest);
        }
        return AjaxResult.error("查询市 失败", -1);
    }

    //查询县
    @RequestMapping(value = "/queryXian", method = RequestMethod.POST)
    public AjaxResult queryXian(@RequestBody Map<String, Object> param) {
        List<Map<String, Object>> rest = wpzfNdhcAPPWyjzService.queryXian(param);
        if (rest != null) {
            return AjaxResult.success("查询县 成功", rest);
        }
        return AjaxResult.error("查询县 失败", -1);
    }

    // 用户注册 申请
    // 县级填报（县级外业人员）12 县级填报+外业管理员 28 platform_id 1 role_id
    // 市级审核（市级外业人员）14 市级填报+外业管理员 30 platform_id 2 role_id
    // 省级审核人员 13 省级填报+外业管理员 29 platform_id 2 role_id
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public AjaxResult register(@RequestBody Map<String, Object> param) {
        Integer rest = wpzfNdhcAPPWyjzService.register(param);
        if (rest > 0) {
            return AjaxResult.success("申请 成功", rest);
        } else if (rest == -1) {
            return AjaxResult.error("用户名称已存在", rest);
        } else {
            return AjaxResult.error("申请 失败", -1);
        }
    }

    //查询省级需要审核的注册用户
    @RequestMapping(value = "/queryReviewUsers", method = RequestMethod.POST)
    public AjaxResult queryReviewUsers(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("xzqdm", loginUser.getSysUser().getXzqdm());
        String xzqdmjq = loginUser.getSysUser().getXzqdmjq();
        String[] xzqdmjqArray = null;
            if (xzqdmjq != null && !"".equals(xzqdmjq)) {
                xzqdmjqArray = xzqdmjq.split(",");
            }
        param.put("xzqdmjqArray", xzqdmjqArray);
        List<Map<String, Object>> rest = wpzfNdhcAPPWyjzService.queryReviewUsers(param);
        Integer cnt = wpzfNdhcAPPWyjzService.queryReviewUsersCount(param);
        if (rest != null) {
            return AjaxResult.success("查询 成功", rest);
        }
        return AjaxResult.error("查询 失败", -1);
    }

    //省级审核用户注册
    @RequestMapping(value = "/reviewUsers", method = RequestMethod.POST)
    public AjaxResult reviewUsers(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("sh_shr", loginUser.getSysUser().getNickName());
        param.put("sh_username", loginUser.getSysUser().getUserName());
        param.put("create_by", loginUser.getSysUser().getUserName());

//        param.put("platform_id",1);
//        param.put("role_id",12);
//        param.put("dept_id",115);

        Integer rest = wpzfNdhcAPPWyjzService.reviewUsers(param);
        if (rest > 0) {
            return AjaxResult.success("用户注册 成功", rest);
        } else if (rest == -1) {
            return AjaxResult.error("用户名称已存在", rest);
        } else {
            return AjaxResult.error("用户注册 失败", -1);
        }
    }

    @RequestMapping(value = "/queryMsg", method = RequestMethod.POST)
    public AjaxResult queryMsg(@RequestBody Map<String, Object> param) {
        String rest = wpzfNdhcAPPWyjzService.queryMsg(param);
        return AjaxResult.success("成功", rest);
    }

}
